package cn.com.xinjinfu.pdoulcProduct.activity.login;

import android.content.Intent;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Map;
import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.activity.base.BaseActivity;
import cn.com.xinjinfu.pdoulcProduct.activity.user.ActCertification;
import cn.com.xinjinfu.pdoulcProduct.engine.ComAsk;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.manager.AccountManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ActivityManager;
import cn.com.xinjinfu.pdoulcProduct.manager.JSONManager;
import cn.com.xinjinfu.pdoulcProduct.manager.L;
import cn.com.xinjinfu.pdoulcProduct.manager.TitleManager;
import cn.com.xinjinfu.pdoulcProduct.utils.NewUtil;

/**
 * 忘记密码
 * Created by android-adt on 2016/1/25.
 */
public class ForgetPwdActivity extends BaseActivity implements View.OnClickListener {
    private String mobile;
    private TextView tv_mobile,tv_error_hint01,tv_error_hint02;
    private EditText etpw,etpw2,etcode;
    private Button btcode;
    private Button register_register_bt;
    private RequestQueue requen;
    private String passwd,passwd1,code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_forget_pwd);
        TitleManager.showTitle(this, "忘记密码", "");
        ActivityManager.getInstance().addActivity(this);
        Intent intent = getIntent();
        mobile = intent.getStringExtra("mobile"); //取得手机号
        intiViews();
        requen = Volley.newRequestQueue(this);
    }
    protected void intiViews() {
        tv_error_hint01 = (TextView) findViewById(R.id.tv_error_hint01);
        tv_error_hint02 = (TextView) findViewById(R.id.tv_error_hint02);

        tv_mobile = (TextView) findViewById(R.id.tv_mobile);
        tv_mobile.setText(mobile.substring(0, 3) + "****" + mobile.substring(7));

        etpw=(EditText)findViewById(R.id.et_pw);
        etpw2=(EditText)findViewById(R.id.et_pw2);
        etcode=(EditText)findViewById(R.id.et_code);//验证码

        etpw.addTextChangedListener(mTextWatcher);
        etpw2.addTextChangedListener(mTextWatcher);
        etcode.addTextChangedListener(mTextWatcher);

        btcode=(Button)findViewById(R.id.bt_code);  //获得验证码
        btcode.setOnClickListener(this);
        GradientDrawable myGrad = (GradientDrawable)btcode.getBackground();
        myGrad.setColor(0xFF6ED3D6);
        register_register_bt=(Button)findViewById(R.id.register_register_bt);
        register_register_bt.setOnClickListener(this);
    }

    public void onClick(View v) {
        if (!NewUtil.checkClick(v.getId())) return;
        switch (v.getId()) {
            case R.id.bt_code:
                ComAsk.getVerifyCode(mobile, this, btcode,"2");
                break;
            case R.id.register_register_bt:
                passwd = etpw.getText().toString();
                passwd1 = etpw2.getText().toString();
                code = etcode.getText().toString();
                if (passwd.length() > 12 || passwd.length() < 6) {
                    tv_error_hint02.setVisibility(View.VISIBLE);
                    tv_error_hint02.setText("密码长度应为6-12位字符");
                }else if (!passwd.equals(passwd1)){
                    tv_error_hint02.setVisibility(View.VISIBLE);
                    tv_error_hint02.setText("两次密码不一致");
                }else {
                    loginRequen();
                }
                break;
        }
    }


    public void loginRequen(){
        final Map<String, String> parameters = DataHandler.getParameters("1005");
        parameters.put("code", code);
        parameters.put("mobile", mobile);
        parameters.put("type", "1");
        parameters.put("newpwd", DataHandler.encrypt3DES(passwd));

        JsonObjectRequest request = new JsonObjectRequest(DataHandler.getBuildUrl(parameters), null, succeedRespon, error);
        requen.add(request);
    }

    private Response.ErrorListener error = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            DataHandler.errorHandler(ForgetPwdActivity.this, error);
        }
    };

    private Response.Listener<JSONObject> succeedRespon = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject jobj) {
            L.i("忘记密码" + jobj.toString());
            if(JSONManager.getError(jobj) == -1){
               finish();
            }else{
                tv_error_hint02.setVisibility(View.VISIBLE);
                tv_error_hint02.setText(JSONManager.getMsg(jobj));
            }

        }
    };
    TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int arg1, int arg2,
                                      int arg3) {
        }
        @Override
        public void onTextChanged(CharSequence s, int arg1, int arg2,
                                  int arg3) {
        }
        @Override
        public void afterTextChanged(Editable s) {
            tv_error_hint02.setVisibility(View.GONE);
            if (TextUtils.isEmpty(etcode.getText()) || TextUtils.isEmpty(etpw.getText()) ||
                    TextUtils.isEmpty(etpw2.getText())) {
                register_register_bt.setEnabled(false);
            } else {
                register_register_bt.setEnabled(true);
            }
        }
    };


}
